using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Elev.UI.Controls
{
    public class FlatTabControl : TabControl
    {
        public FlatTabControl()
        {
            // double buffering
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            DrawMode = TabDrawMode.Normal;
            
            Padding = new Point(0, 0);
            Margin = new Padding(0);
        }

        /// <summary>
        /// Returns the index of the tab at the given location
        /// </summary>
        public int GetTabIndexAtPoint(Point pt)
        {
            for (int i = 0; i < TabCount; i++)
            {
                Rectangle r = GetTabRect(i);
                if (r.Contains(pt))
                {
                    return i;
                }
            }
            return -1;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            DrawControl(e.Graphics);
        }

        void DrawControl(Graphics g)
        {
            if (!Visible) return;

            Rectangle TabControlArea = ClientRectangle;
            Rectangle TabArea = DisplayRectangle;

            // Fill background color of the entire TagControl control
            using (SolidBrush sb = new SolidBrush(SystemColors.MenuBar))
            {
                g.FillRectangle(sb, TabControlArea);
            }

            // Fill background of the visible Tab rectangle
            using (SolidBrush sb = new SolidBrush(Color.WhiteSmoke))
            {
                TabArea.Inflate(4, 4);
                g.FillRectangle(sb, TabArea);
            }

            for (int i = 0; i < TabCount; i++)
                DrawTab(g, i);
        }

        void DrawTab(Graphics g, int index)
        {
            TabPage tp = TabPages[index];
            Rectangle tpArea = GetTabRect(index);
            tpArea.Offset(-2, 0);
            
            Color back = SystemColors.Window;
            if (index != SelectedIndex)
            {
                back = Color.Transparent;
            }

            // Draw the tab back color
            using (SolidBrush sb = new SolidBrush(back))
            {
                g.FillRectangle(sb, tpArea);
            }

            // Draw the tab top line & borders
            if (index == SelectedIndex)
            {
                using (Pen p = new Pen(Color.OrangeRed, 3))
                {
                    g.DrawLine(p, new Point(tpArea.X, tpArea.Y), new Point(tpArea.X + tpArea.Width, tpArea.Y));
                }
            }

            // Draw string
            g.DrawString(tp.Text, Font, new SolidBrush(Color.Black), tpArea.X + 4, tpArea.Y + 2);
        }
    }
}
